﻿layui.define(['layer'], function (exports) {
    var $ = layui.jquery;
    var layer = layui.layer;


    requirejs.config({
        baseUrl: "/Content/editormd-master/",
        paths: {
            jquery: "examples/js/jquery.min",
            marked: "lib/marked.min",
            prettify: "lib/prettify.min",
            raphael: "lib/raphael.min",
            underscore: "lib/underscore.min",
            sequenceDiagram: "lib/sequence-diagram.min",
            flowchart: "lib/flowchart.min",
            jqueryflowchart: "lib/jquery.flowchart.min",
            katex: "lib/katex.min",
            editormd: "editormd.amd" // Using Editor.md amd version for Require.js
        },
        shim: {
            "editormd": [
                'jquery'
                , 'marked'
                , 'prettify'
                , 'raphael'
                , 'underscore'
                , 'sequenceDiagram'
                , 'flowchart'
                , 'jqueryflowchart'
                , 'katex'
            ]
            , "jqueryflowchart": ['flowchart']
            , "sequenceDiagram": ['raphael']
        }
        , waitSeconds: 30
    });

    var docEditor;
    var deps = [
        "editormd",
        "languages/en",
        "plugins/link-dialog/link-dialog",
        "plugins/reference-link-dialog/reference-link-dialog",
        "plugins/image-dialog/image-dialog",
        "plugins/code-block-dialog/code-block-dialog",
        "plugins/table-dialog/table-dialog",
        "plugins/emoji-dialog/emoji-dialog",
        "plugins/goto-line-dialog/goto-line-dialog",
        "plugins/help-dialog/help-dialog",
        "plugins/html-entities-dialog/html-entities-dialog",
        "plugins/preformatted-text-dialog/preformatted-text-dialog",
    ];
    docEditorResize = function () {
        if (docEditor) {
            docEditor.resize("100%", 750);
            console.log('GetDetail=>docEditor.resize("100%", 750)');
        }
    } 
    var InitEditorMD = function (md) {
        require(deps, function (editormd) {
            
            editormd.loadCSS("/Content/editormd-master/lib/codemirror/addon/fold/foldgutter");

            docEditor = editormd("test-editormd", {
                width: "100%",
                height: 750,
                path: '/Content/editormd-master/lib/',
                markdown: md,
                codeFold: true,
                searchReplace: true,
                saveHTMLToTextarea: true,                // 保存HTML到Textarea
                htmlDecode: "style,script,iframe|on*",       // 开启HTML标签解析，为了安全性，默认不开启
                emoji: true,
                taskList: true,
                tex: true,
                tocm: true,         // Using [TOCM]
                autoLoadModules: false,
                previewCodeHighlight: true,
                flowChart: false,
                sequenceDiagram: false,
                //dialogLockScreen : false,   // 设置弹出层对话框不锁屏，全局通用，默认为true
                //dialogShowMask : false,     // 设置弹出层对话框显示透明遮罩层，全局通用，默认为true
                //dialogDraggable: true,    // 设置弹出层对话框不可拖动，全局通用，默认为true
                //dialogMaskOpacity: 0.4,    // 设置透明遮罩层的透明度，全局通用，默认值为0.1
                //dialogMaskBgColor: "#000", // 设置透明遮罩层的背景颜色，全局通用，默认为#fff
                //imageUpload: true,
                //imageFormats: ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
                //imageUploadURL: "/WikiManage/WikiDocument/Upload",
                onload: function () {
                    console.log('onload', this);
                    //this.fullscreen();
                    //this.unwatch();
                    //this.watch().fullscreen();

                    //this.setMarkdown("#PHP");
                    //this.width("100%");
                    //this.height(480);

                    this.resize("100%", 750);
                    this.on('paste', function () {
                        console.log('upload_image');
                    });
                    //layer.close(loadingIndex);
                }
            });
        });
    }
    var ling_markdown = {
        doc: docEditor, init: InitEditorMD
    };
    //对外接口
    exports('ling_markdown', ling_markdown);
});